package com.iphanta.community.remote.service.impl;

import com.iphanta.community.common.message.CommunityMessage;
import com.iphanta.community.common.utils.JsonUtils;
import com.iphanta.community.remote.feign.TecentImFeign;
import com.iphanta.community.remote.service.TimService;
import com.iphanta.community.remote.utils.TLSSigAPIv2;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author 墨声 E-mail: zehong.hongframe.huang@gmail.com
 * create time: 4/16/22 2:10 PM
 */
@Service
public class TimServiceImpl implements TimService {

    private static final Logger LOGGER = LoggerFactory.getLogger(TimServiceImpl.class);

    @Autowired
    private TecentImFeign tecentImFeign;

    @Autowired
    private RedissonClient redissonClient;

    private Long sdkappid = 1400664495L;

    private String key = "a201f130d3e13e5d4a6cf5eeef50ffa87f7328c45c2222ba1d950cef428117fb";

    private String identifier = "administrator";

    private Random random = new Random();

    @Override
    public void createUser(String uid, String nickname, String photo) {
        TLSSigAPIv2 sigAPIv2 = new TLSSigAPIv2(this.sdkappid, this.key);
        Random random = new Random();
        Map<String, Object> request = new HashMap<>();
        request.put("UserID", uid);
        request.put("Nick", nickname);
        request.put("FaceUrl", photo);
        Object o = this.tecentImFeign.accountImport(
                this.sdkappid,
                this.identifier,
                sigAPIv2.genUserSig(this.identifier, 1000000L),
                random.nextInt(10000000),
                "json",
                request);
        LOGGER.info("{}", JsonUtils.INSTANCE.toJsonString(o));

    }

    @Override
    public void sendMessage(String from, String to, CommunityMessage message) {

        TLSSigAPIv2 sigAPIv2 = new TLSSigAPIv2(this.sdkappid, this.key);
        Random random = new Random();

        /**
         * {
         *     "SyncOtherMachine": 2, // 消息不同步至发送方
         *     "From_Account": "lumotuwe2",
         *     "To_Account": "lumotuwe2",
         *     "MsgLifeTime":60, // 消息保存60秒
         *     "MsgSeq": 93847636,
         *     "MsgRandom": 1287657,
         *     "MsgTimeStamp": 1557387418,
         *     "ForbidCallbackControl":[
         *         "ForbidBeforeSendMsgCallback",
         *         "ForbidAfterSendMsgCallback"], // 禁止回调控制选项
         *     "MsgBody": [
         *         {
         *             "MsgType": "TIMTextElem",
         *             "MsgContent": {
         *                 "Text": "hi, beauty"
         *             }
         *         }
         *     ],
         *     "CloudCustomData": "your cloud custom data"
         * }
         *
         */

        Map<String, Object> request = new HashMap<>();
        request.put("SyncOtherMachine", 2);
        request.put("From_Account", from);
        request.put("To_Account", to);
        request.put("MsgSeq", message.getMessageSeq());
        request.put("MsgRandom", this.random.nextInt(1000000));
        request.put("MsgTimeStamp", System.currentTimeMillis()/1000L);

        Map<String, Object> body = new HashMap<>();
        body.put("MsgType", "TIMTextElem");

        Map<String, Object> MsgContent = new HashMap<>();
        MsgContent.put("Text", JsonUtils.INSTANCE.toJsonString(message));
        body.put("MsgContent", MsgContent);
        List<Object> msgBody = new ArrayList<>();
        msgBody.add(body);
        request.put("MsgBody", msgBody);

        Object o = this.tecentImFeign.sendMessage(
                this.sdkappid,
                this.identifier,
                sigAPIv2.genUserSig(this.identifier, 1000000L),
                random.nextInt(10000000),
                "json",
                request);
        LOGGER.info("TIM sendMessage result: {}", JsonUtils.INSTANCE.toJsonString(o));
    }
}
